﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESHOP.Domain;
using System.Data.Common;

namespace ESHOP.DAO
{
    public class CONGTYDAO :BaseDAO
    {
      
        public CONGTY Get(String ma)
        {
            return _db.CONGTies.Where(p => p.ID .Equals(ma) && p.ISACTIVE==true).SingleOrDefault();
        }
        public List<CONGTY> GetList()
        {
            return _db.CONGTies.Where(p=>p.ISACTIVE==true).ToList();
        }
        public int Update(CONGTY objui)
        {
            int result = 1;
            try
            {
                var objdb = Get(objui.ID);
                if (objdb != null)
                {
                    objdb.TENCONGTY = objui.TENCONGTY;
                    objdb.WEBSITE = objui.WEBSITE;
                    objdb.MADM = objui.MADM;
                    objdb.MAQUOCGIA = objui.MAQUOCGIA;
                    objdb. Email = objui.Email;
                    objdb.MOTA = objui.MOTA;
                    objdb.KEYWORD = objui.KEYWORD;
                    //objdb.ISACTIVE = objui.ISACTIVE;
                    _db.SubmitChanges();
                }
            }
            catch
            {
                result = 0;
            }
            return result;
        }
        public int Insert(CONGTY objui)
        {
            int result = 1;
            DbTransaction trans = null;
            try
            {
                _db.Connection.Open();
                trans = _db.Connection.BeginTransaction();
                _db.Transaction = trans;
                _db.CONGTies.InsertOnSubmit(objui);
                _db.SubmitChanges();
                trans.Commit();

            }
            catch
            {
                result = 0;
            }
            return result;
        }
        public int Delete(CONGTY objui)
        {
            int result = 1;
            try
            {
                var objdb = Get(objui.ID);
                if (objdb != null)
                {
                    _db.CONGTies.DeleteOnSubmit(objdb);
                    _db.SubmitChanges();
                }
            }
            catch
            {
                result = 0;
            }
            return result;
        }

        public string NewId()
        {
            string sReturn = "";

            string sToday = "";
            sToday = DateTime.Now.Year.ToString();
            sToday = DateTime.Now.Month < 10
                         ? sToday + "0" + DateTime.Now.Month.ToString()
                         : sToday + DateTime.Now.Month.ToString();

            sToday = DateTime.Now.Day < 10
                         ? sToday + "0" + DateTime.Now.Day.ToString()
                         : sToday + DateTime.Now.Day.ToString();

            sReturn = (from p in _db.CONGTies.Where(p => p.ID.Substring(0, 8).Contains(sToday))
                       select p.ID).Max();

            if (!string.IsNullOrEmpty(sReturn))
            {
                int thutu = int.Parse(sReturn.Substring(8, 3));
                thutu = thutu + 1;
                if (thutu < 10)
                {
                    sReturn = sToday + "00" + thutu.ToString();
                }
                else if (thutu < 100)
                {
                    sReturn = sToday + "0" + thutu.ToString();
                }
                else if (thutu < 1000)
                {
                    sReturn = sToday + thutu.ToString();
                }
            }
            else
            {
                sReturn = sToday + "001";
            }

            return sReturn;
        }
    }
}
